package structures.pane;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import analyzers.InputAnalyzer;

import constants.Globals;

import structures.xmltable.ServiceParameterTable;
import util.HibernateUtil;
import xml.sax.esi.EsiXMLProcessor;
import xml.sax.plm.PlmInXMLProcessor;
import domain.XmlNodeDAO;
import exception.IBToolsException;

public class XmlPaneNode_new implements java.io.Serializable, Globals{
	
	private static final long serialVersionUID = 8822624614042781302L;
	
	private int paneIndex;
	private XmlNodeDAO xmlNode;
	private boolean selectedToCompare;
	
	private XmlPane containerPane;
	private String outXml;
	
	private String nodeFromOtherSource;

	public XmlPaneNode_new(XmlPane containerPane, XmlNodeDAO xmlNode, int paneIndex) throws IBToolsException{
		this.paneIndex = paneIndex;
		this.containerPane = containerPane;
		this.xmlNode = xmlNode;
	}
	
	public String getXmlstring(){
		String xmlstring = null;
		if(xmlNode != null)
			xmlstring = xmlNode.getXmlString();
		else
			xmlstring = "";
		return xmlstring;
	}

	public ServiceParameterTable getAsTable() throws IBToolsException {
		ServiceParameterTable table = null;
		String type = InputAnalyzer.analyzeXmlType(xmlNode.getXmlString(), true);
		if(type.equals(XMLTYPE_RC))
			table = PlmInXMLProcessor.convertXmlToTable(xmlNode.getXmlString());
		else if(type.equals(XMLTYPE_ESI))
			table = EsiXMLProcessor.convertXmlStringToTable(xmlNode.getXmlString());
		return table;
	}
	public boolean isDeleteButtonVisible(){
		return xmlNode.getOid() != null && xmlNode.getOid() > 0 && xmlNode.getStatus();
	}
	public boolean isSaveButtonVisible(){
		boolean visible = xmlNode.getOid() == null || xmlNode.getOid() < 1 || !xmlNode.getStatus();
		visible = visible ? visible : containerPane.getSelectedToUpdateIndex() == this.paneIndex;
		return visible;
	}
	public boolean isCloseButtonVisible(){
		return xmlNode == null || xmlNode.getOid() == null || xmlNode.getOid() < 1;
	}
	public String getSessionState() {
		if( !containerPane.isInPane()){
			if(selectedToCompare)
				return "updating";
			return "outnode";
		}
		if(xmlNode != null){
			if(containerPane.getSelectedToUpdateIndex() == this.paneIndex){
				return "updating";
			}
			else if(nodeFromOtherSource != null){
				return nodeFromOtherSource;
			}
			else if(xmlNode.getOid() != null && xmlNode.getOid() > 0){
				if(xmlNode.getStatus())
					return "persisted";
				else
					return "deleted";
			}
		}
		return "new";
	}
	private void persist() throws IBToolsException{
    	Session session = HibernateUtil.getDEVSession();
        Transaction tx = null;
        
        try {
			tx = HibernateUtil.beginDEVTransaction(session);
			if(xmlNode.getOid() != null)
				session.merge(xmlNode);
			else
				session.saveOrUpdate(xmlNode);
			tx.commit();
		} catch (HibernateException e) {
			tx.rollback();
			throw e;
		}
	}

	public String getVisualServiceName(int size){
		String name = "";
		if(getServiceName() != null){
			if(getServiceName().length() > size){
				name += getServiceName().substring(0, size) + "...";
			}
			else {
				name += getServiceName();
			}
		}
		return name;
	}
	public String getServiceName(){
		return xmlNode.getServiceName();
	}
	
	void evict() throws IBToolsException{
    	Session session = HibernateUtil.getDEVSession();
    	Transaction tx = null;
    	try {
			tx = HibernateUtil.beginDEVTransaction(session);
	        session.evict(xmlNode);
			tx.commit();
			
		} catch (HibernateException e) {
			tx.rollback();
			throw e;
		}
	}
	void activate(String xmlString, String serviceName, long projectOid) throws IBToolsException{
		xmlNode.setStatus(true);
		if(xmlString != null){
			xmlNode.setXmlString(xmlString.trim());
			xmlNode.setServiceName(serviceName);
//			xmlNode.setProject(new ProjectInfoDAO(projectOid));
		}
		persist();
	}
	void delete() throws IBToolsException{
		xmlNode.setStatus(false);
		persist();
		evict();
		
		xmlNode.setStatus(true);
		xmlNode.setOid(null);
	}
	public boolean nodeIsSelectedToUpdate(){
		return containerPane.getSelectedToUpdateIndex() == this.paneIndex;
	}
	public boolean isSelectedToCompare() {
		return selectedToCompare;
	}
	public void setSelectedToCompare(boolean selectedToCompare) {
		this.selectedToCompare = selectedToCompare;
	}
	public int getPaneIndex() {
		return paneIndex;
	}
	public void setPaneIndex(int paneIndex) {
		this.paneIndex = paneIndex;
	}
	public String getOutXml() {
		return outXml;
	}
	public void setOutXml(String outXml) {
		this.outXml = outXml;
	}
	public String getNodeFromOtherSource() {
		return nodeFromOtherSource;
	}
	public void setNodeFromOtherSource(String nodeFromOtherSource) {
		this.nodeFromOtherSource = nodeFromOtherSource;
	}

	public String getSourceClass() {
		return nodeFromOtherSource;
	}
	public boolean isContainsOutput(){
		return outXml != null;
	}
}
